package com.itzgyw.example.controller;

import com.itzgyw.cache.annotation.CacheClear;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/7/20 14:33
 * @Description: 暂无描述
 */
@RestController
@Slf4j
public class LoginController {
    @Autowired
    private UserService userService;


    @PostMapping(path = "/get")
    public ResultModel get(@RequestBody Request request) throws IOException {
        return this.data(userService.get(request.getId()));
    }

    @PostMapping(path = "/list")
    public ResultModel list(@RequestBody Request request) throws IOException {
        return this.data(userService.list(request.getPage(), 4));
    }

    @PostMapping(path = "/page")
    public ResultModel page(@RequestBody Request request) throws IOException {
        return this.data(userService.page(request.getPage(), 4));
    }

    @PostMapping(path = "/update")
    public ResultModel reloadNull(@RequestBody Request request) {
        userService.update1(request.getUser());
        return this.ok();
    }

    @PostMapping(path = "/insert")
    public ResultModel insert(@RequestBody Request request) {
        userService.insert(request.getUser());
        return this.ok();
    }

    @PostMapping(path = "/delete")
    public ResultModel delete(@RequestBody Request request) {
        userService.delete(request.getId());
        return this.ok();
    }

    @PostMapping(path = "/clear")
    @CacheClear
    public ResultModel clear() throws IOException {
        return this.data("SUCCESS");
    }

    public ResultModel data(Object data) {
        ResultModel resultModel = new ResultModel<>(0, "success");
        return resultModel.flashData(data);
    }

    public ResultModel ok() {
        ResultModel resultModel = new ResultModel<>(0, "success");
        return resultModel;
    }
}
